package com.project.generation;

import com.project.database.DataBaseData;
import com.project.util.Config;
import com.project.util.IoUtil;
import com.project.util.Util;
import lombok.SneakyThrows;

import java.io.File;
import java.net.URI;

public class ProjectGeneration {

    private Config config;
    private DataBaseData dataBaseData;
    private JavaGeneration javaGeneration;
    private ResourcesGeneration resourcesGeneration;
    private WebAppGeneration webAppGeneration;

    public ProjectGeneration(Config config) {
        this.config = config;
        this.dataBaseData = new DataBaseData(
                config.getDriver(),
                config.getUrl(),
                config.getUsername(),
                config.getPassword());
        this.javaGeneration = new JavaGeneration( this.config, this.dataBaseData );
        this.resourcesGeneration = new ResourcesGeneration(this.config, this.dataBaseData);
        this.webAppGeneration = new WebAppGeneration(this.config, this.dataBaseData);
    }

    public void start() {
        this.javaGeneration.start();
        this.resourcesGeneration.start();
        this.webAppGeneration.start();
        this.otherGeneration();
    }

    @SneakyThrows
    private void otherGeneration() {
        // 获取controller模板字符串
        URI pomTemplateURL = this.getClass().getClassLoader().getResource("template-pom.xml").toURI();
        File file = new File(pomTemplateURL);
        String pomTemplate = IoUtil.readFile(file);
        pomTemplate = pomTemplate.replaceAll(Util.getTemplateString("databaseName"), dataBaseData.getDatabaseName());
        IoUtil.write(pomTemplate, this.config.getOutPath()+"\\"+this.config.getProjectName()+"\\"+"pom.xml");
    }


}
